﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FieldServices_01;

namespace FieldServices
{
    public partial class frmWorkItems : UIFramework.UIForm
    {
        private int OrderID;
        private DataTable dt = null;

        public frmWorkItems()
        {
            InitializeComponent();
            InitializeListItem();
        }

        private void InitializeListItem()
        {
            #region Initialize layout
            DataTable _dt = new DataTable();
            _dt.Columns.Add("OrderID", typeof(string));
            _dt.Columns.Add("Customer", typeof(string));
            _dt.Columns.Add("Status", typeof(string));

            //Oder ID
            DataGridTextBoxColumn ColOrder = new DataGridTextBoxColumn();
            ColOrder.MappingName = "OrderID";
            ColOrder.HeaderText = "Order";
            ColOrder.Width = 120;
            ColOrder.Format = "c";
            //ColOrder.Alignment = HorizontalAlignment.Left;
            //ColOrder.ReadOnly = true;

            //Customer name
            DataGridTextBoxColumn ColCustomer = new DataGridTextBoxColumn();
            ColCustomer.MappingName = "Customer";
            ColCustomer.HeaderText = "Customer";
            ColCustomer.Width = 260;
            ColCustomer.Format = "c";
            //ColCustomer.Alignment = HorizontalAlignment.Left;
            //ColCustomer.ReadOnly = true;

            //Status
            DataGridTextBoxColumn ColStatus = new DataGridTextBoxColumn();
            ColStatus.MappingName = "Status";
            ColStatus.HeaderText = "Status";
            ColStatus.Width = 80;
            ColStatus.Format = "c";
            //ColStatus.Alignment = HorizontalAlignment.Left;
            //ColStatus.ReadOnly = true;
            #endregion

            if (Common.bchoise == true)
            {
                #region SAP
                Common._Workitems();
                foreach (MboWorkitems wk in Common.workItems)
                {

                    DataRow dr = _dt.NewRow();
                    dr.Delete();
                    dr[0] = wk.ORDERID + "\n" + wk.ORDER_TYPE;
                    dr[1] = wk.NAME + "\n" + wk.POSTL_CODE + " " + wk.STREET;

                    // change to status image
                    dr[2] = wk.SYSTEM_STATUS;
                    _dt.Rows.Add(dr);
                }
                #endregion
            }

            else
            {
                #region Sample
                DataSet ds = Common.dsItems;
                dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = _dt.NewRow();
                    dr.Delete();
                    string Order = String.Format("{0} {1}", 
                                          dt.Rows[i]["ORDERID"].ToString(), 
                                          dt.Rows[i]["ORDER_TYPE"].ToString());

                    string Customer = String.Format("{0} {1}", 
                                          dt.Rows[i]["NAME"].ToString(), 
                                          dt.Rows[i]["PARTNER"].ToString());

                    dr[0] = Order;
                    dr[1] = Customer;
                    dr[2] = dt.Rows[i]["SYSTEM_STATUS"].ToString();
                    _dt.Rows.Add(dr);

                }               
                #endregion
            }
            DataGridTableStyle aGridTableStyle = new DataGridTableStyle();
            aGridTableStyle.GridColumnStyles.Add(ColOrder);
            aGridTableStyle.GridColumnStyles.Add(ColCustomer);
            aGridTableStyle.GridColumnStyles.Add(ColStatus);
            _dt.TableName = "WorkingItems";
            gridItems.DataSource = _dt;
            gridItems.TableStyles.Add(aGridTableStyle);
            aGridTableStyle.MappingName = "WorkingItems";
            //CurrencyManager cm = (CurrencyManager)this.BindingContext[gridItems.DataSource, gridItems.DataMember];
            //((DataView)cm.List).AllowEdit = true;
            //((DataView)cm.List).AllowNew = false; 
        }

        private void lst_view_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (lstView.SelectedIndices.Count == 0)
            //       return;
            // int i = lstView.SelectedIndices.Count;
            // string Order = String.Format("{0} {1}", dt.Rows[i]["ORDERID"].ToString(), dt.Rows[i]["ORDER_TYPE"].ToString());
            // string Customer = String.Format("{0} {1}", dt.Rows[i]["NAME"].ToString(), dt.Rows[i]["PARTNER"].ToString());
            //frmServicesOrder OrderDetail = new frmServicesOrder(Order, Customer);
            //OrderDetail.Show();
        }

        private void btnNotification_Click(object sender, EventArgs e)
        {
            frmNotificationDetail frm = new frmNotificationDetail();
            frm.Show();

        }

        private void picBack_Click(object sender, EventArgs e)
        {

        }

        private void btnDetail_Click(object sender, EventArgs e)
        {

        }

        private void gridItems_DoubleClick(object sender, EventArgs e)
        {
            int row = gridItems.CurrentRowIndex;
            string orderID = dt.Rows[row]["OrderID"].ToString();
            frmServicesOrder OrderDetail = new frmServicesOrder(orderID);
            OrderDetail.Show();

        }

    }
}